const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = function (api, params) {
  const { getWebpackConfig } = api;
  const config = getWebpackConfig();
  const dir = process.cwd();

  /** 新增入口文件 */
  config.entry("login").add(path.resolve(dir, "./src/login.js"));

  /** ejs */
  config.module
    .rule("ejs")
    .test(/\.ejs$/)
    .exclude.add(/node_modules/)
    .end()
    .use("ejs-loader")
    .loader("ejs-loader")
    .options({
      esModule: false,
    });

  config.plugin("HtmlWebpackPlugin").tap((args) => {
    const [arg] = args;
    return [{ ...arg, template: path.resolve(dir, "./src/index.html") }];
  });

  config.plugin("HtmlWebpackPlugin2").use(HtmlWebpackPlugin, [
    {
      filename: "login.html",
      template: path.resolve(dir, "./src/login.html"),
      chunks: ["login"],
    },
  ]);

  config.plugin("ProvidePlugin").use(webpack.ProvidePlugin, [
    {
      $: "jquery",
      jQuery: "jquery",
    },
  ]);

  config.plugin("CopyWebpackPlugin").use(CopyWebpackPlugin, [
    {
      patterns: [
        {
          from: path.resolve(dir, "./src/img"),
          to: path.resolve(dir, "./dist/img"),
        },
      ],
    },
  ]);
};
